package com.xm.xmapp;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.RequestInterFaceCode;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.ClearEditText;
import com.xm.xmapp.dialog.SubmitBankInfoDialog;
import com.xm.xmapp.dialog.SubmitBankInfoDialog.OnSubmitBankInfoDialog;
import com.xm.xmapp.dialog.SumbitBankMoneyDialog;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Apply_refund_moneyRequest;
import com.xm.xmapp.request.bean.Get_bank_listRequest;
import com.xm.xmapp.request.bean.RequestRefundTotalListDetails;
import com.xm.xmapp.response.bean.ResponseApplyRefundCondition;
import com.xm.xmapp.response.bean.ResponseBankListBean;
import com.xm.xmapp.response.bean.ResponseBankListBean.T;
import com.xm.xmapp.response.bean.ResponseMoneyListRefund;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;

public class SumbitBankInfoActivity extends BaseActivity implements OnClickListener {

	private TextView submit, bankName, havename, tv_bk_pay, tv_wx_gzh, tv_wx_pay, tv_ali_pay, tv_info;
	private ClearEditText name, bank;
	private Apply_refund_moneyRequest requestBean;
	private ResponseBankListBean bean;
	private LinearLayout ll_ali_pay, ll_wx_pay, ll_bk_pay, ll_wx_gzh;// 支付宝，微信，银行卡跟布局点击事件控件
	private ResponseApplyRefundCondition mBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sumbitbankinfo);
	}

	@Override
	public void initView() {

		submit = (TextView) findViewById(R.id.submit);
		bankName = (TextView) findViewById(R.id.bankName);
		havename = (TextView) findViewById(R.id.havename);
		name = (ClearEditText) findViewById(R.id.name);
		bank = (ClearEditText) findViewById(R.id.bank);
		tv_info = (TextView) findViewById(R.id.tv_info);
		ll_ali_pay = (LinearLayout) findViewById(R.id.ll_ali_pay);
		ll_wx_pay = (LinearLayout) findViewById(R.id.ll_wx_pay);
		ll_bk_pay = (LinearLayout) findViewById(R.id.ll_bk_pay);
		ll_wx_gzh = (LinearLayout) findViewById(R.id.ll_wx_gzh);
		tv_bk_pay = (TextView) findViewById(R.id.tv_bk_pay);
		tv_wx_gzh = (TextView) findViewById(R.id.tv_wx_gzh);
		tv_wx_pay = (TextView) findViewById(R.id.tv_wx_pay);
		tv_ali_pay = (TextView) findViewById(R.id.tv_ali_pay);
	}

	@Override
	public void initData() {

		setMyTitle("押金退款");
		name.setMoren(false);
		requestBean = new Apply_refund_moneyRequest();
		String cash_amount = getIntent().getStringExtra("cash_amount");
		requestBean.cash_amount = cash_amount;
		String cash_type = getIntent().getStringExtra("cash_type");
		requestBean.cash_type = cash_type;
		mBean = (ResponseApplyRefundCondition) getIntent().getSerializableExtra("bean");
		getData();

		String str_text = "";
		if (cash_type.equals("1")) {
			str_text = "您的车辆押金为" + cash_amount + "元";
		} else {
			str_text = "您的违章押金为" + cash_amount + "元";
		}
		SpannableStringBuilder textStyle = StringTools.getStyle(7, cash_amount + "元", str_text);
		tv_info.setText(textStyle);

		if (mBean.user_name == null || mBean.user_name.equals("")) {
			name.setVisibility(View.VISIBLE);
			havename.setVisibility(View.GONE);
		} else {
			name.setVisibility(View.GONE);
			havename.setVisibility(View.VISIBLE);
			havename.setText(mBean.user_name);
		}
		// tv_bk_pay, tv_wx_gzh, tv_wx_pay, tv_ali_pay
		if (mBean.total != null) {
			if (mBean.total.alipay <= 0) {
				ll_ali_pay.setVisibility(View.GONE);
			} else {
				tv_ali_pay.setText(mBean.total.alipay + "元");
			}
			if (mBean.total.wx_app <= 0) {
				ll_wx_pay.setVisibility(View.GONE);
			} else {
				tv_wx_pay.setText(mBean.total.wx_app + "元");
			}
			if (mBean.total.wx_gzh <= 0) {
				ll_wx_gzh.setVisibility(View.GONE);
			} else {
				tv_wx_gzh.setText(mBean.total.wx_gzh + "元");
			}
			if (mBean.total.bank <= 0) {
				ll_bk_pay.setVisibility(View.GONE);
				findViewById(R.id.ll_bottom).setVisibility(View.GONE);

			} else {
				findViewById(R.id.ll_bottom).setVisibility(View.VISIBLE);
				tv_bk_pay.setText(mBean.total.bank + "元");
			}
		}

	}

	private void getData() {
		Get_bank_listRequest requestBean1 = new Get_bank_listRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean1, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				bean = JSON.parseObject(response, ResponseBankListBean.class);
				bankName.setText(bean.list.get(0).getText());
				requestBean.bank_id = bean.list.get(0).getId();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	@Override
	public void initEvent() {

		submit.setOnClickListener(this);
		bankName.setOnClickListener(this);
		ll_ali_pay.setOnClickListener(this);
		ll_wx_pay.setOnClickListener(this);
		ll_bk_pay.setOnClickListener(this);
		ll_wx_gzh.setOnClickListener(this);
		bank.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s == null || s.length() == 0)
					return;
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < s.length(); i++) {
					if (i != 4 && i != 9 && i != 14 && i != 19 && s.charAt(i) == ' ') {
						continue;
					} else {
						sb.append(s.charAt(i));
						if ((sb.length() == 5 || sb.length() == 10 || sb.length() == 15 || sb.length() == 20)
								&& sb.charAt(sb.length() - 1) != ' ') {
							sb.insert(sb.length() - 1, ' ');
						}
					}
				}

				if (!sb.toString().equals(s.toString())) {
					int index = start + 1;
					if (sb.charAt(start) == ' ') {
						if (before == 0) {
							index++;
						} else {
							bank.setText(sb.subSequence(0, sb.length() - 1));
							index--;
						}
					} else {
						if (before == 1) {
							index--;
						}
					}
					bank.setText(sb.toString());
					bank.setSelection(index);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case CodeConstant.BankCode:
			ResponseBankListBean.T userTimeBean = (T) data.getSerializableExtra("bean");
			bankName.setText(userTimeBean.getText());
			requestBean.bank_id = userTimeBean.getId();
			break;

		default:
			break;
		}

	}

	@Override
	public void onClick(View v) {
		// 1=微信公众号，2=支付宝，6=微信app
		switch (v.getId()) {
		case R.id.ll_wx_pay:
			getMoneyList(RequestInterFaceCode.Payment_type.WX_APP);
			break;
		case R.id.ll_bk_pay:
			break;
		case R.id.ll_wx_gzh:
			getMoneyList(RequestInterFaceCode.Payment_type.WX_GZH);
			break;
		case R.id.ll_ali_pay:
			getMoneyList(RequestInterFaceCode.Payment_type.ALIPAY);
			break;
		case R.id.bankName: {
			if (bean != null) {
				intent = new Intent(MyAppcation.getMyAppcation(), BankSelectActivity.class);
				intent.putExtra("bean", bean);
				startActivityForResult(intent, CodeConstant.BankCode);
			} else {
				getData();
			}

			break;
		}
		case R.id.submit: {
			if (mBean.total.bank > 0) {

				if (mBean.user_name == null || mBean.user_name.equals("")) {
					mBean.user_name = name.getText().toString().trim();
				}
				if (mBean.user_name.equals("")) {
					Tools.showMessage("持卡人姓名不能为空");
				} else if (!StringTools.isBankNumber(bank.getText().toString().replaceAll(" ", ""))) {
					Tools.showMessage("请填写正确的银行卡号");
				} else {
					setRequestData();

				}
			} else {
				setRequestData();
			}
			break;
		}
		default:
			break;
		}
	}

	/**
	 * 设置请求参数
	 */
	private void setRequestData() {

		boolean isHaveBank;

		if (mBean.total.bank > 0) {
			isHaveBank = true;
		} else {
			isHaveBank = false;
		}
		SubmitBankInfoDialog submitBankInfoDialog = new SubmitBankInfoDialog(this, isHaveBank);
		submitBankInfoDialog.show();
		submitBankInfoDialog.setOnSubmitBankInfoDialog(new OnSubmitBankInfoDialog() {

			@Override
			public void refuse() {
			}

			@Override
			public void accept() {
				if (mBean.total.bank > 0) {
					requestBean.bank_card_user_name = mBean.user_name.replaceAll(" ", "");
					requestBean.bank_card_number = bank.getText().toString().replaceAll(" ", "");
				} else {
					requestBean.bank_card_user_name = "";
					requestBean.bank_card_number = "";
					requestBean.bank_id = "";
				}
				submitData();
			}
		});
	}

	/**
	 * 支付方式 ：1=微信公众号，2=支付宝，6=微信app
	 */

	private void getMoneyList(final int payment_type) {

		RequestRefundTotalListDetails requestBean = new RequestRefundTotalListDetails();
		requestBean.payment_type = payment_type;
		requestBean.cash_type = this.requestBean.cash_type;
		switch (payment_type) {
		case RequestInterFaceCode.Payment_type.WX_GZH:
			requestBean.cash_type_total = mBean.total.wx_gzh;
			break;
		case RequestInterFaceCode.Payment_type.ALIPAY:
			requestBean.cash_type_total = mBean.total.alipay;
			break;
		case RequestInterFaceCode.Payment_type.WX_APP:
			requestBean.cash_type_total = mBean.total.wx_app;
			break;

		default:
			break;
		}
	
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				List<ResponseMoneyListRefund> list = JSON.parseArray(response, ResponseMoneyListRefund.class);
				SumbitBankMoneyDialog mDialog = new SumbitBankMoneyDialog(payment_type, SumbitBankInfoActivity.this,
						list);
				mDialog.show();

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	protected void submitData() {

		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				// 申请退款类型 ：1=车辆押金，2=违章押金
				finish();
				MyAppcation.getMyAppcation().notifyDataSetChanged(ReturnMoneyActivity.class, Type.A_ReturnMoney.FINISH,
						0);
				MyAppcation.getMyAppcation()
						.notifyDataSetChanged(MyWalletActivity.class, Type.A_MyWallet.GET_WALLET, 0);
				int temp = Integer.parseInt(requestBean.cash_type.toString());
				if (temp == RequestInterFaceCode.Apply_refund_money.CAR) {
					Tools.showMessage("申请成功，车辆押金将于5天内退回到您的账户");
				} else {
					Tools.showMessage("申请成功，违章押金押金将于5天内退回到您的账户");
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

}
